/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ljsystem;

import databaseManager.DbConn;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 47
 */
public class UserManager {

    public UserManager() {
    }
    public String msg = "";
    public boolean deleteUser(String[] uids){
        boolean isOK = false;
        Connection conn = null;
        Statement stmt = null;
        String sql = "";
        int i = 0;
        if (uids.length > 0) {
            try {
                conn = DbConn.getNewDbconn();
                stmt = conn.createStatement();
                conn.setAutoCommit(false);
                for (i = 0; i < uids.length; i++) {
                    uids[i] = new String(uids[i].getBytes("ISO-8859-1"), "UTF-8");
                    sql = "delete from usertable where user_id=" + uids[i];
                    stmt.execute(sql);
                }
                conn.commit();
                conn.setAutoCommit(true);
                TopMenuManager.resetMenus();
                this.msg = "删除成功";
                isOK = true;
            } catch (Exception e) {
                isOK = false;
                try {
                    conn.rollback();
                } catch (Exception connre) {
                }
                this.msg = e.getMessage();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception stmte) {
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception conne) {
                    }
                }
            }
        }
        return isOK;
    }

    public boolean modifyUser(String id, String name, String pass, String memo) {
        boolean isOK = false;
        Connection conn = null;
        Statement stmt = null;
        String sql = "";
        if (id != null && name != null && pass != null && memo != null) {
            try {
                id = new String(id.getBytes("ISO-8859-1"), "UTF-8");
                name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
                pass = new String(pass.getBytes("ISO-8859-1"), "UTF-8");
                memo = new String(memo.getBytes("ISO-8859-1"), "UTF-8");
                sql = "update usertable";
                sql += " set user_name='" + name + "',";
                sql += "user_password='" + pass + "',";
                sql += "memo='" + memo + "'";
                sql += " where user_id=" + id;
                conn = DbConn.getNewDbconn();
                stmt = conn.createStatement();
                stmt.execute(sql);
                TopMenuManager.resetMenus();
                this.msg = "修改成功";
                isOK = true;
            } catch (Exception e) {
                isOK = false;
                this.msg = e.getMessage();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception stmte) {
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception conne) {
                    }
                }
            }
        }
        return isOK;
    }

    public bo.User getUserById(String id) {
        if (id == null) {
            return null;
        }
        bo.User u = null;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String sql;
        try {
            id = new String(id.getBytes("ISO-8859-1"), "UTF-8");
            sql = "select u.user_id,u.user_password,u.user_name,ifnull(u.memo,'') as memo from usertable u where u.user_id=" + id;
            conn = DbConn.getNewDbconn();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if (rs.next()) {
                u = new bo.User();
                u.userId = rs.getLong("user_id");
                u.memo = rs.getString("memo");
                u.setUserName(rs.getString("user_name"));
                u.setUserPassword(rs.getString("user_password"));
            }
        } catch (Exception ex) {
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception rse) {
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception stmte) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception conne) {
                }
            }
        }
        return u;
    }

    public boolean addNewUser(String uname, String upass, String memo) {
        boolean isOK = false;
        Connection conn = null;
        Statement stmt = null;
        String sql = null;
        if (uname != null && upass != null && memo != null) {
            try {
                uname = new String(uname.getBytes("ISO-8859-1"), "UTF-8");
                upass = new String(upass.getBytes("ISO-8859-1"), "UTF-8");
                memo = new String(memo.getBytes("ISO-8859-1"), "UTF-8");
                sql = "insert into usertable(user_name,user_password,memo) values";
                sql += "('" + uname + "','" + upass + "','" + memo + "')";
                conn = DbConn.getNewDbconn();
                stmt = conn.createStatement();
                stmt.execute(sql);
                TopMenuManager.resetMenus();
                this.msg = "添加成功";
                isOK = true;
            } catch (Exception e) {
                isOK = false;
                this.msg = e.getMessage();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception stmte) {
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception conne) {
                    }
                }
            }
        }
        return isOK;
    }

    public List getAllUsers() {
        List list = new ArrayList();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        bo.User user = null;
        String sql = "";
        try {
            sql = "select u.user_id,u.user_password,u.user_name,ifnull(u.memo,'') as memo from usertable u order by user_name";
            conn = DbConn.getNewDbconn();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);//获得所有用户组信息

            while (rs.next()) {//组织列表
                user = new bo.User();
                user.userId = rs.getLong("user_id");
                user.memo = rs.getString("memo");
                user.setUserName(rs.getString("user_name"));
                user.setUserPassword(rs.getString("user_password"));
                list.add(user);
            }
        } catch (Exception ex) {
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception rse) {
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception stmte) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception conne) {
                }
            }
        }
        return list;
    }
}
